package ru.budget.mobile.data.repository

import ru.budget.mobile.core.Response
import ru.budget.mobile.data.entities.IncomeData
import ru.budget.mobile.data.sources.IncomeLocalSource
import ru.budget.mobile.domain.entities.IncomeDomain
import ru.budget.mobile.domain.repository.IncomeRepository

class IncomeRepositoryImpl(
    private val local: IncomeLocalSource
): IncomeRepository {

    override suspend fun incomeList(): Response<List<IncomeDomain>> =
        local.incomeList().map {
            it.map(mapper)
        }

    override suspend fun addIncome(income: IncomeDomain): Response<Boolean> =
        local.addIncome(
            income = IncomeData(
                id = income.id,
                sum = income.sum,
                datetime = income.datetime
            )
        )

    private val mapper = { income: IncomeData ->
        IncomeDomain(
            id = income.id,
            sum = income.sum,
            datetime = income.datetime
        )
    }
}